﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Specialized;
using System.Web;
using MsExchangeBcsConnector.ExchangeBdcModel.Helpers;

namespace MsExchangeBcsConnector
{
    internal enum SearchConnectorEntityType
    {
        Root,
        MailBox,
        MailFolder,
        Message, 
        Attachment
    }

    

    internal class SearchConnectorUriParser
    {

        internal string EMail { get; private set; }

        internal string MailFolderID { get; private set; }

        internal string MessageID { get; private set; }

        internal string AttachmentID { get; private set; }

        internal SearchConnectorEntityType EntityType { get; private set; }

        internal static SearchConnectorUriParser ParseAndValidateUriParams(Uri uri)
        {
            SearchConnectorUriParser result = new SearchConnectorUriParser();

            if (uri == null || string.IsNullOrEmpty(uri.Host))
            {
                result.EntityType = SearchConnectorEntityType.Root;
                return result;
            }

           NameValueCollection queryParams = HttpUtility.ParseQueryString(uri.Query);

           if (queryParams != null && queryParams.Count >0){

               result.EMail = queryParams[Constants.MailBoxEntityName]; // EMail required in all case

               if (!string.IsNullOrEmpty(queryParams[Constants.AttachmentEntityName]))
                {
                    
                    result.EntityType = SearchConnectorEntityType.Attachment;                    
                    result.MessageID = Base32Url.FromBase32String(queryParams[Constants.MessageEntityName]);
                    result.AttachmentID = Base32Url.FromBase32String(queryParams[Constants.AttachmentEntityName]);
                   
                }else if (!string.IsNullOrEmpty(queryParams[Constants.MessageEntityName]))
                {
                    result.EntityType = SearchConnectorEntityType.Message;                    
                    result.MessageID = Base32Url.FromBase32String(queryParams[Constants.MessageEntityName]);
                   
                }else if (!string.IsNullOrEmpty(queryParams[Constants.MailFolderEntityName]))
                {
                    result.EntityType = SearchConnectorEntityType.MailFolder;
                    result.MailFolderID = Base32Url.FromBase32String(queryParams[Constants.MailFolderEntityName]);
                   

                }else if (!string.IsNullOrEmpty(queryParams[Constants.MailBoxEntityName]))
                {
                    result.EntityType = SearchConnectorEntityType.MailBox;
               
                }                
                
           }else if (uri.Host.Equals(Constants.MailBoxRootName, StringComparison.InvariantCultureIgnoreCase)){
                
               result.EntityType = SearchConnectorEntityType.Root;

            }else{
                    throw new ArgumentException(@"Illegal Entity {0}. Address should be in format msews://root ", uri.Host);
            }
            

            return result;
        }

    }
}
